//
//  NewFeatureViewController.swift
//  Weibo
//
//  Created by apple on 15/12/19.
//  Copyright © 2015年 Leon. All rights reserved.
//

import UIKit
import SnapKit

private let imageCount = 4
private let reuseIdentifier = "Cell"

class NewFeatureViewController: UICollectionViewController {
    
    //不用写override 只能重写父类的方法 不能通过父类去找爷爷类的构造方法
    init() {
        //指定流水布局
        let layout = UICollectionViewFlowLayout()
        //修改布局
        layout.itemSize = UIScreen.mainScreen().bounds.size
        //修改视图滚动方式
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        //设置item上下左右的间距
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        super.init(collectionViewLayout: layout)
        //开启分页
        collectionView?.pagingEnabled = true
        //取消弹簧效果
        collectionView?.bounces = false
        //取消滚动条
        collectionView?.showsHorizontalScrollIndicator = false
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        
    }
    
    
    // MARK: UICollectionViewDataSource
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return imageCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
        
        //cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.redColor() : UIColor.blueColor()
        cell.index = indexPath.item
        return cell
    }
    
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        //indexPath 是上一个item的的indexpath 不能直接使用
        //获取准确的cell
        let cell = collectionView.visibleCells().last! as! NewFeatureCell
        let index = collectionView.indexPathForCell(cell)!
        print(index.item)
        if index.item == imageCount - 1 {
            cell.showAnimation()
        }
    }
    
}


class NewFeatureCell: UICollectionViewCell {
    
    
    
    //添加index属性
    var index:Int = 0 {
        didSet {
            //相当于oc中重写set方法
            iconView.image = UIImage(named: "new_feature_\(index + 1)")
            //隐藏 开始按钮
//            startBtn.hidden = index != 3
            startBtn.hidden = true
//            self.showAnimation()
        }
    }
    
    private func showAnimation() {
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        
        startBtn.hidden = false
        /**
        衰变的动画效果
        iOS7.0推出的
        
        - parameter <Tduration:             动画持续时间
        - parameter delay:                  动画延迟时间
        - parameter usingSpringWithDamping: 弹簧系数 区间 0.1 ~ 1  越小越弹
        - parameter initialSpringVelocity:  加速度
        - parameter options:                动画的可选项  swift中使用数组
        - parameter animations:             执行动画闭包
        - parameter completion:             动画执行完成的闭包
        */
        
        UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            self.startBtn.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                print("OK")
        }
    }
    
    @objc private func startBtnDidClick() {
        //动画结束的时候 发出 页面切换的通知
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVCNotification, object: nil)
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        //自定义的子视图 需要添加到 contenView上
        contentView.addSubview(iconView)
        contentView.addSubview(startBtn)
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
        
        //设置开始按钮的约束
        startBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView.snp_centerX)
            //VFL  constant
            make.bottom.equalTo(contentView.snp_bottom).offset(-180)
        }
        
        //添加按钮的点击事件
        startBtn.addTarget(self, action: "startBtnDidClick", forControlEvents:.TouchUpInside)
        
    }
    //懒加载
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    private lazy var startBtn: UIButton = {
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
        btn.setTitle("开始体验", forState: .Normal)
        btn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        
        //设置大小
        btn.sizeToFit()
        return btn
    }()
    
}


